package com.android.fragmentdemo
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import androidx.recyclerview.widget.RecyclerView
import com.android.fragmentdemo.adapters.CustomVerticalLayoutManagerAdapter
import com.hengfoxwolf.commonbase.base.BaseActivity
import com.android.fragmentdemo.databeans.DataFactoryIp
import com.android.fragmentdemo.databinding.ActivityVerticalCustomLayoutManagerBinding
import com.android.fragmentdemo.definervlayoutmanager.LooperLayoutManager3
import com.android.fragmentdemo.defineviews.LooperVerticalListDataItemDecoration
import com.android.fragmentdemo.snaphelper.ViewPagerSnapHelper
import java.lang.ref.WeakReference

class VerticalCustomLayoutManagerActivity :
    BaseActivity<ActivityVerticalCustomLayoutManagerBinding>() {

    private var mRecycler: RecyclerView.Recycler? = null

    private var mState: RecyclerView.State? = null

    private val mHandler by lazy {
        HHHandler(this@VerticalCustomLayoutManagerActivity)
    }

    override fun getLayoutId(): Int = R.layout.activity_vertical_custom_layout_manager

    override fun init(savedInstanceState: Bundle?) {
        val dataSet = DataFactoryIp.createCharStr(15)

        val mLayoutManger = LooperLayoutManager3()

        mBinding.rvVerticalLm.apply {
            //layoutManager = CustomHorizontalLayoutManager()
            //layoutManager = LooperLayoutManager(true)
            layoutManager = mLayoutManger

            adapter = CustomVerticalLayoutManagerAdapter().apply {
                itemDataSet = dataSet
            }

            //addItemDecoration(HorizontalListDataItemDecoration(context).apply {
            //    setDrawLastLine(true)
            //})

            setRecyclerListener { holder ->
                //判断item是否回收
                val layoutPosition = holder.layoutPosition
                Log.d(MINE_TAG, "layoutPosition:$layoutPosition")
            }

            addItemDecoration(LooperVerticalListDataItemDecoration(context).apply {
                setDrawLastLine(true)
            })
        }

        ViewPagerSnapHelper().attachToRecyclerView(mBinding.rvVerticalLm)

        //mHandler.sendEmptyMessageDelayed(MSG_DO_TRANSLATE, 2000)

        //反射获取RecyclerView中的对象
        val mRecyclerFiled = RecyclerView::class.java.getDeclaredField("mRecycler")
        mRecyclerFiled.isAccessible = true
        mRecycler =
            mRecyclerFiled.get(mBinding.rvVerticalLm) as? RecyclerView.Recycler

        //反射获取RecyclerView中的对象
        val mRecyclerStateFiled = RecyclerView::class.java.getDeclaredField("mState")
        mRecyclerStateFiled.isAccessible = true
            mState =  mRecyclerStateFiled.get(mBinding.rvVerticalLm) as? RecyclerView.State
    }

    override fun onDestroy() {
        super.onDestroy()
        mHandler.removeCallbacksAndMessages(null)
    }

    private fun doFakeScroll() {
        //mBinding.rvHorizontalLm.scrollBy(MIN_TRANSLATE_VALUE, 0)
        mBinding.rvVerticalLm.scrollBy(0, -MIN_TRANSLATE_VALUE)
    }

    private class HHHandler(activity: VerticalCustomLayoutManagerActivity) : Handler(Looper.getMainLooper()) {
        private val weakReference = WeakReference(activity)
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            val tempActivity = weakReference.get() ?: return
            when (msg.what) {
                MSG_DO_TRANSLATE -> {
                    tempActivity.doFakeScroll()
                    tempActivity.mHandler.sendEmptyMessageDelayed(MSG_DO_TRANSLATE, 500)
                }
            }
        }
    }

    companion object {
        const val MSG_DO_TRANSLATE = 0x01
        const val MIN_TRANSLATE_VALUE = 50
        const val MINE_TAG = "VerticalCustom"
    }
}